<?php defined('SYSPATH') or die('No direct script access.');

class arrays {

    public static function subArray($orginalArray) {
        $msgArgs = func_get_args();
        $subArray = array();
        for ($i = 1; $i < count($msgArgs); $i++) {
            $key = $msgArgs[$i];
            $subArray[$key] = $orginalArray[$key];
        }
        return $subArray;
    }
    
    
    /**
     * Creates new map from the $originalMap, which includes only keys from $keyList.
     */
    public static function filterMap(&$originalMap, $keyList) {
        $filteredMap = array();
        foreach ($keyList as $key) {
            if (array_key_exists($key, $originalMap)) {
                $filteredMap[$key] = $originalMap[$key];
            }
        }
        return $filteredMap;
    }
    
    public static function mapFromListOfArrays($listOfArrays, $valueFieldName, $labelFieldName) {
    	$result = array();
    	foreach ($listOfArrays as $row) {
    		$result[$row[$valueFieldName]] = $row[$labelFieldName]; 
    	}
    	return $result;
    }
    
    /** Picks a single field value from each array and makes a new list out of it. */
    public static function listFromListOfArrays($listOfArrays, $fieldName) {
    	$result = array();
    	foreach ($listOfArrays as $row) {
    		$result[] = $row[$fieldName];
    	}
    	return $result;
    }
}
